package util.yahoo;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;

import org.apache.commons.digester.Digester;

import vo.yahoo.ListMessageResponse;
import vo.yahoo.Message;
import vo.yahoo.MessageInfo;
import vo.yahoo.MessagePart;

public class MessageBodyDigester {

	public static void main(String[] args) {
	    System.setProperty("org.apache.commons.logging.Log", "org.apache.commons.logging.impl.SimpleLog");  
	    System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.digester.Digester", "debug");  
	    System.setProperty("org.apache.commons.logging.simplelog.log.org.apache.commons.digester.Digester.sax", "info");  
		InputStream is = MessageBodyDigester.class.getResourceAsStream("/util/yahoo/Message.xml");
		BufferedReader reader = new BufferedReader ( new InputStreamReader ( is) );
		StringBuffer sbText = new StringBuffer();
		String line = null ;
        try {
			while ((line = reader.readLine()) != null) {
				sbText.append(line);
			}
			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
        Message resp = (Message) parse(sbText.toString());
	}
	
	public static Object parse(String listMessageResponse) {
		Message resp = null;
		try {
			Digester digester = new Digester();
			digester.setValidating(false);

			digester.addObjectCreate(
					"SOAP-ENV:Envelope/SOAP-ENV:Body/GetMessageResponse/message",
					Message.class);
			digester.addBeanPropertySetter(
					"SOAP-ENV:Envelope/SOAP-ENV:Body/GetMessageResponse/message/mid",
					"mid");
			digester.addBeanPropertySetter(
					"SOAP-ENV:Envelope/SOAP-ENV:Body/GetMessageResponse/message/receivedDate",
					"receivedDate");
			digester.addBeanPropertySetter(
					"SOAP-ENV:Envelope/SOAP-ENV:Body/GetMessageResponse/message/subject", 
					"subject");
			digester.addBeanPropertySetter(
					"SOAP-ENV:Envelope/SOAP-ENV:Body/GetMessageResponse/message/from/email",
					"fromEmail");
			digester.addBeanPropertySetter(
					"SOAP-ENV:Envelope/SOAP-ENV:Body/GetMessageResponse/message/from/name",
					"fromName");
			digester.addBeanPropertySetter(
					"SOAP-ENV:Envelope/SOAP-ENV:Body/GetMessageResponse/message/replyto/name", 
					"replyToName");
			digester.addBeanPropertySetter(
					"SOAP-ENV:Envelope/SOAP-ENV:Body/GetMessageResponse/message/replyto/email", 
					"replyToEmail");
			digester.addBeanPropertySetter(
					"SOAP-ENV:Envelope/SOAP-ENV:Body/GetMessageResponse/message/to/name",
					"toName");
			digester.addBeanPropertySetter(
					"SOAP-ENV:Envelope/SOAP-ENV:Body/GetMessageResponse/message/to/email",
					"toEmail");
			digester.addObjectCreate(
					"SOAP-ENV:Envelope/SOAP-ENV:Body/GetMessageResponse/message/part",
					MessagePart.class);
			digester.addSetProperties(
					"SOAP-ENV:Envelope/SOAP-ENV:Body/GetMessageResponse/message/part",
					"partId","partId");
			digester.addSetProperties(
					"SOAP-ENV:Envelope/SOAP-ENV:Body/GetMessageResponse/message/part",
					"type", "type");
			digester.addSetProperties(
					"SOAP-ENV:Envelope/SOAP-ENV:Body/GetMessageResponse/message/part",
					"subtype","subtype");
			digester.addSetProperties(
					"SOAP-ENV:Envelope/SOAP-ENV:Body/GetMessageResponse/message/part",
					"typeParams", "typeParams");
			digester.addSetProperties(
					"SOAP-ENV:Envelope/SOAP-ENV:Body/GetMessageResponse/message/part",
					"disposition", "disposition");
			digester.addSetProperties(
					"SOAP-ENV:Envelope/SOAP-ENV:Body/GetMessageResponse/message/part",
					"dispParams", "dispParams");
			digester.addSetProperties(
					"SOAP-ENV:Envelope/SOAP-ENV:Body/GetMessageResponse/message/part",
					"encoding", "encoding");
			digester.addSetProperties(
					"SOAP-ENV:Envelope/SOAP-ENV:Body/GetMessageResponse/message/part",
					"fileName", "fileName");
			digester.addSetProperties(
					"SOAP-ENV:Envelope/SOAP-ENV:Body/GetMessageResponse/message/part",
					"size", "size");
			digester.addSetProperties(
					"SOAP-ENV:Envelope/SOAP-ENV:Body/GetMessageResponse/message/part",
					"isTruncated", "isTruncated");
			digester.addBeanPropertySetter(
					"SOAP-ENV:Envelope/SOAP-ENV:Body/GetMessageResponse/message/part/text",
					"text");
			digester.addSetNext("SOAP-ENV:Envelope/SOAP-ENV:Body/GetMessageResponse/message/part", "addPart", "vo.yahoo.MessagePart");
			digester.addBeanPropertySetter(
					"SOAP-ENV:Envelope/SOAP-ENV:Body/GetMessageResponse/message/xapparentlyto", 
					"xapparentlyto");
			
			StringReader sr = new StringReader(listMessageResponse);
			resp = (Message) digester.parse(sr);

		} catch (Exception exc) {
			exc.printStackTrace();
		}
		return resp;
	}
}